package helen.core.tools.appmonitor;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import helen.core.tools.utils.FileUtils;

/**
 * 以标签形式记录应用程序性能的类,主要是用来打时间戳,记录网络请求流量
 * Created by cssnote on 2015/10/30.
 */
public class AppMonitor {
    private class TimeTag{
        public String tag;
        public String description;
    }

    private class NetTaskTag{
        public String Tag;
        public int length;
    }

    private static AppMonitor monitor =null;
    private HashMap<String ,Long> startTime;
    private List<TimeTag> tags ;
    private List<NetTaskTag> nets;

    public void putNet(String tag, int length){
        NetTaskTag monitor = new NetTaskTag();
        monitor.Tag = tag;
        monitor.length = length;
        nets.add(monitor);
    }

    public static  AppMonitor getInstance(){
        if(monitor==null){
            monitor =  new AppMonitor();
            return monitor;
        }
        return monitor;
    }
    private AppMonitor (){
        startTime = new HashMap<>();
        tags = new ArrayList<>();
        nets = new ArrayList<>();
    }


    /**
     * 压入时间节点
     * @param tag
     */
    public void push(String tag){
        if(!MonitorControl.APP_MOMITOR){
            return;
        }
        long start = System.currentTimeMillis();
        startTime.put(tag,start);
    }

    /**
     * 抛出时间节点
     * @param tag
     */
    public void pop(String tag){
        if(!MonitorControl.APP_MOMITOR){
            return;
        }
        Long start = startTime.get(tag);
        if(start == null){
            TimeTag timetag = new TimeTag();
            timetag.tag = tag;
            timetag.description = "-1";
            tags.add(timetag);
            return ;
        }

        long end = System.currentTimeMillis();
        TimeTag timetag = new TimeTag();
        timetag.tag = tag;
        timetag.description = (end-start)+"";
        tags.add(timetag);
        startTime.remove(tag);
        return ;
    }

    public List<TimeTag> getList(){
        return tags;
    }

    public void clean (){
        tags.clear();
    }

    public void printLog(String path,String fileName){
        if(!MonitorControl.APP_MOMITOR){
            return;
        }
        if(fileName == null){
            Calendar ca = Calendar.getInstance();
            fileName = "time_log_";
            fileName += ca.get(Calendar.YEAR)+"-"+ca.get(Calendar.MONTH)+"-"+ca.get(Calendar.DATE)+"_";
            fileName += ca.get(Calendar.HOUR)+":"+ca.get(Calendar.MINUTE)+":"+ca.get(Calendar.SECOND)+".txt";
        }
        FileUtils fileUtils = new FileUtils();
        fileUtils.createSdDir(path);
        String log = "";
        for(int i=0;i<tags.size();i++){
            log += "tag:"+tags.get(i).tag+"\n";
            log += "description:"+tags.get(i).description+"\n\n";
        }
        fileUtils.write2SdFromInput(path, fileName, fileUtils.String2InputStream(log));
    }
    public void printNetLog(String path,String fileName){
        if(!MonitorControl.APP_MOMITOR){
            return;
        }
        if(fileName == null){
            Calendar ca = Calendar.getInstance();
            fileName = "net_log_";
            fileName += ca.get(Calendar.YEAR)+"-"+ca.get(Calendar.MONTH)+"-"+ca.get(Calendar.DATE)+"_";
            fileName += ca.get(Calendar.HOUR)+":"+ca.get(Calendar.MINUTE)+":"+ca.get(Calendar.SECOND)+".txt";
        }
        FileUtils fileUtils = new FileUtils();
        fileUtils.createSdDir(path);
        String log = "";
        int sum=0;
        for(int i=0;i<nets.size();i++){
            log += "tag:"+nets.get(i).Tag+"\n";
            log += "length:"+nets.get(i).length+"\n\n";
            sum += nets.get(i).length;
        }
        log += "-----------------------------------------------\n";
        log += "total length:"+sum+"\n";
        log += "-----------------------------------------------\n";
        fileUtils.write2SdFromInput(path,fileName,fileUtils.String2InputStream(log));
    }
}
